package com.jyh.supermark.dao.impl;

import com.jyh.db.BaseDAO;
import com.jyh.supermark.dao.GoodsDao;
import com.jyh.supermark.domain.BuyCar;
import com.jyh.supermark.domain.Goods;
import java.util.List;

public class GoodsDaoImpl implements GoodsDao {
    //找数据库中存在的商品编号
    @Override
    public List<Goods> selectByGoodsId(String id) {
        String sql="select * from goods where id=?";
        List<Goods> goodsList = BaseDAO.selectMultipleRow(sql, Goods.class, id);
        if (goodsList.size()>0){
            return goodsList;
        }
        return null;
    }

    //把要购买的商品信息添加(封装)到购物车
    public int insertBuyCar(BuyCar buyCar){//10101,,,10   10101,,,20
        List<BuyCar> buyCarList = selectId(buyCar.getId());
        if (buyCarList==null){
            String sql="insert into buyCar value(?,?,?,?)";
            int count = BaseDAO.insertOneRow(sql,buyCar.getId(),buyCar.getName(),buyCar.getPrice(),buyCar.getBuynum());
            return count;
        }
        else {
            Integer buyNum = BaseDAO.selectMultipleRow("select buyNum from buyCar where id=?", BuyCar.class, buyCar.getId()).get(0).getBuynum();
            String sql="update BuyCar set buyNum=? where id=?";
            int count1 = BaseDAO.updateMultipleRow(sql, buyNum + buyCar.getBuynum(), buyCar.getId());
            return count1;
        }
    }

    // 更新商品库存信息
    public int updateGoodsStockInfo(String id, Integer updateStock) {
        String sql = "update goods set stock=? where id=?";
        return BaseDAO.updateMultipleRow(sql, updateStock, id);
    }

    //查询有无已经添加到购物车的商品信息
    public List<BuyCar> selectId(String id){
        String sql="select id from BuyCar where id=?";
        List<BuyCar> buyCarList = BaseDAO.selectMultipleRow(sql, BuyCar.class, id);
        if (buyCarList.size()>0){
            return buyCarList;
        }
        return null;
    }
}
